package club.dmyang.controller;

import club.dmyang.common.enums.RespCodeEnums;
import club.dmyang.common.vo.ResposeVo;
import club.dmyang.entity.Comment;
import club.dmyang.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.HtmlUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * Created by brss on 2018/8/1.
 */
@Controller
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private CommentService commentService;

    /**
     * 留言板点赞
     * @param commentId
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/vote")
    public ResposeVo voteOneComment(Integer commentId,HttpServletRequest request){
        //获取Sessionid
        String sessionId = request.getSession().getId() + "_vote";
        if (redisTemplate.opsForValue().get(sessionId) != null){
            return new ResposeVo(RespCodeEnums.RESP_CODE_ERROR.getCode(), "点赞失败,3秒内只能点赞一次!");
        }
        //把Sessionid装入redis，并且设置3秒过期
        redisTemplate.opsForValue().set(sessionId,sessionId,3, TimeUnit.SECONDS);

        int i = commentService.voteOneComment(commentId);
        if ( i < 1 ){
            return new ResposeVo(RespCodeEnums.RESP_CODE_ERROR.getCode(), "点赞失败");
        }
        return new ResposeVo(RespCodeEnums.RESP_CODE_SUCC.getCode(),"点赞成功");
    }

    /**
     * 向留言板添加一条留言
     * @param commentContent
     * @return
     */

    @ResponseBody
    @RequestMapping("/comment")
    public ResposeVo commentOneArticle(HttpServletRequest httpServletRequest,
                                       HttpServletResponse httpServletResponse,
                                       String commentContent,
                                       String vericode){
        //验证验证码
        String captchaId = (String) httpServletRequest.getSession().getAttribute("vrifyCode");
        String parameter = httpServletRequest.getParameter("vericode");
        System.out.println("Session  vrifyCode "+captchaId+" form vrifyCode "+parameter);

        if (!captchaId.equals(parameter)) {
            return new ResposeVo(RespCodeEnums.RESP_CODE_ERROR.getCode(),"错误的验证码");
        }
        HttpSession session = httpServletRequest.getSession();
        //判断留言内容是否为空
        if (commentContent.equals(""))
            return new ResposeVo(RespCodeEnums.RESP_CODE_ERROR.getCode(), "内容不能为空，评论失败！");

        Comment comment = new Comment();
        comment.setArtiId(-1);
        comment.setContent(HtmlUtils.htmlEscape(commentContent));
        comment.setCreateTime(new Date());
        comment.setVotes(0);
        Comment c = commentService.addOneComment(comment);
        if (c==null)
            return new ResposeVo(1, "评论失败！");

        //评论成功，返回刚才评论的对象
        return new ResposeVo(RespCodeEnums.RESP_CODE_SUCC.getCode(),
                "评论成功！",
                RespCodeEnums.NOT_A_LIST.getCode(),c);
    }

}
